package com.study.java.jinjie.file.lianxi;

import java.io.*;
import java.util.List;

public class Score {
    public Score(List<Student> chengji) {
        this.chengji = chengji;
    }

    private List<Student> chengji ;


    //创建一个文件夹Student
    //Student.txt写入内容
    public void pingJun() {
        String dir = "d:\\testDir\\student";
        File file = new File(dir);
        try {
            file.mkdir();
            FileWriter writer = new FileWriter(dir+"\\student.txt");
            writer.write("序号\t"+"     学号\t"+"   姓名\t"+"   成绩");
            writer.write(System.lineSeparator());
            writer.write("------------------------------");
            writer.write(System.lineSeparator());
            for (int i = 0; i < chengji.size(); i++) {


                writer.write(String.valueOf(i+1)+"     \t"+chengji.get(i).getNum()+
                        "  \t"+chengji.get(i).getName()+"  \t"+chengji.get(i).getChengJi());
                writer.write(System.lineSeparator());
                writer.write("------------------------------");
                writer.write(System.lineSeparator());
            }
            writer.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
    public void pingJun1() {
        String dir = "d:\\testDir\\student";
        File file = new File(dir);
        try {
            file.mkdir();
            BufferedWriter writeText = new BufferedWriter(new FileWriter(dir+"\\student.csv", true));
            writeText.append("序号,"+"学号,"+"姓名,"+"成绩");
            writeText.newLine();    //换行
            for (int i = 0; i < chengji.size(); i++) {
                writeText.append((i+1)+","+chengji.get(i).getNum()+
                        ","+chengji.get(i).getName()+","+chengji.get(i).getChengJi());
                writeText.newLine();    //换行

            }
            writeText.flush();
            writeText.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

}
